package projectStopWatch;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class StopWatch extends JFrame {
	private JButton start, stop, reset;
	private JLabel time;
	private Timer timer;
	private int count = 0;

	public StopWatch() {
		JPanel p1 = new JPanel();
		start = new JButton("Start");
		stop = new JButton("Stop");
		reset = new JButton("Reset");
		time = new JLabel("0.0.0.0", SwingConstants.CENTER);

		p1.setLayout(new GridLayout(1, 3, 5, 5));
		p1.add(start);
		p1.add(stop);
		p1.add(reset);

		add(time, BorderLayout.CENTER);
		add(p1, BorderLayout.SOUTH);

		start.addActionListener(new StartClock());
		stop.addActionListener(new StopClock());
		reset.addActionListener(new ResetClock());
		TimeClass tc = new TimeClass(count);
		timer = new Timer(10, tc);
	}

	public class StartClock implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			timer.start();
		}
	}

	public class StopClock implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			timer.stop();
		}
	}

	public class ResetClock implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			timer.stop();
			time.setText("0:0:0:0");
			TimeClass tc = new TimeClass(count);
			timer = new Timer(10, tc);
		}
	}

	public class TimeClass implements ActionListener {
		int counter;

		public TimeClass(int counter) {
			this.counter = counter;
		}

		public void actionPerformed(ActionEvent e) {
			counter++;
			int hours = (counter / (100 * 60 * 60)) % 60;
			int minutes = (counter / (100 * 60)) % 60;
			int seconds = counter / 100 % 60;
			int miliseconds = counter % 100;
			time.setText(hours + ":" + minutes + ":" + seconds + ":"
					+ miliseconds);
		}
	}

	public static void main(String[] args) {

		StopWatch frame = new StopWatch();
		frame.setTitle("Stopwatch");
		frame.setSize(250, 250);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}